package com.onedec.apply.controller;

import com.onedec.apply.entity.OnedecApply;
import com.onedec.apply.service.OnedecApplyService;
import com.onedec.apply.util.Page;
import com.onedec.apply.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("apply")
public class ApplyController {
    @Autowired
    private OnedecApplyService onedecApplyService;
@RequestMapping("insert")
    public Result inset(OnedecApply onedecApply) throws  Exception{
    //todo  id为登录员工id
    int id=2;
    int i = onedecApplyService.selectQunzhuId(id);
    onedecApply.setApplyEmpId(id);
    onedecApply.setApplyQunzhuId(i);
    onedecApply.setApplyStatus("2");
    onedecApply.setApplyDatetime(new Date());

    onedecApplyService.insert(onedecApply);
    return new Result("200","success",null,null);
}


    @RequestMapping("updateAll")
    @Transactional
    public Result updateAll(@RequestParam(value = "nums[]") Integer[] nums) throws Exception {
        for (Integer i : nums) {
            onedecApplyService.updateById(i);
        }
        return new Result("200", "success", null, null);
    }

    @RequestMapping("approve")
    public Result selectAll(OnedecApply onedecApply, Page page, int num) throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("pageIndex", page.getPageIndex());
        map.put("num", num);
        map.put("applyStatus",onedecApply.getApplyStatus());
        long dataCount = onedecApplyService.accountnum();

        page.setTotalCount(dataCount);
        page.setTotalPage((dataCount - 1) / num + 1);

        List<Map<String, Object>> maps = onedecApplyService.selectByMap(map);


        return new Result("200", "success", page, maps);
    }

    @RequestMapping("updateById")
    public Result updateById(int id) throws Exception {

        onedecApplyService.updateById(id);

        return new Result("200", "success", null, null);
    }

    @RequestMapping("rejectById")
    public Result rejectById(int id) throws Exception {

        onedecApplyService.rejectById(id);

        return new Result("200", "success", null, null);
    }
}
